<?php
/**
 * Created by PhpStorm.
 * User: Jozef Môstka
 * Date: 5/25/14
 * Time: 7:31 PM
 */

require_once "../com/mostka/serializer/SerializerInt.php";
require_once "../com/mostka/serializer/Serializer.php";

require_once "./TestObject.php";
require_once "./TestObject2.php";
require_once "./TestObjectGenerated.php";
require_once "./TestObject2Generated.php";

ob_start();

class Main{

    public static function mains(array $str){


        echo "Start".PHP_EOL;

        $JavaSer = self::testJavaByteArray();

        $testStr = "superTest";
		$testInt = 555555555;
		$negativeInt = -1;
        $testIntArr = [$testInt,$testInt,$testInt,$testInt];
		$testShort = 15645;
		$testShortArr =[$testShort,$testShort,$testShort,$testShort];
		$testlong = PHP_INT_MAX-10;
		$testlongArr = [$testlong,$testlong,$testlong,$testlong];
		$testDouble = 12345.6789;
        $testDoubleArr = [$testDouble,$testDouble,$testDouble,$testDouble];

        self::assert_($JavaSer->readInteger(),$testInt,'integer');
        self::assert_($JavaSer->readInteger(),$negativeInt,'$negativeInt');
        self::assert_($JavaSer->readString(),$testStr,'$testStr');
        self::assert_($JavaSer->readShort(),$testShort,'$testShort');
        self::assert_($JavaSer->readLong(),$testlong,'$testlong');
        self::assert_($JavaSer->readDouble(),$testDouble,'$testDouble');

        self::assert_($JavaSer->readIntegerArr(),$testIntArr,'testIntArr');
        self::assert_($JavaSer->readShortArr(),$testShortArr,'testShortArr');
        self::assert_($JavaSer->readLongArr(),$testlongArr,'testlongArr');
        self::assert_($JavaSer->readDoubleArr(),$testDoubleArr,'readDoubleArr');

        $to = new TestObject();
        $so = TestObjectGenerated::___deserialize($JavaSer);

        self::assert_($to->equals($so),null,'testObject from java');

    }

    private static function testJavaByteArray(){
        $str = file_get_contents("../../output");
        $javaByteArray = [3, -29, 26, 29, 33, 3, -1, -1, -1, -1, 21, 9, 0, 0, 0, 115, 117, 112, 101, 114, 84, 101, 115, 116, 1, 61, 29, 5, -11, -1, -1, -1, -1, -1, -1, 127, 9, -95, -8, 49, -26, -42, 28, -56, 64, 4, 4, 0, 0, 0, -29, 26, 29, 33, -29, 26, 29, 33, -29, 26, 29, 33, -29, 26, 29, 33, 2, 4, 0, 0, 0, 61, 29, 61, 29, 61, 29, 61, 29, 6, 4, 0, 0, 0, -11, -1, -1, -1, -1, -1, -1, 127, -11, -1, -1, -1, -1, -1, -1, 127, -11, -1, -1, -1, -1, -1, -1, 127, -11, -1, -1, -1, -1, -1, -1, 127, 16, 4, 0, 0, 0, -95, -8, 49, -26, -42, 28, -56, 64, -95, -8, 49, -26, -42, 28, -56, 64, -95, -8, 49, -26, -42, 28, -56, 64, -95, -8, 49, -26, -42, 28, -56, 64, 24, 17, 0, 22, 2, 0, 0, 0, 2, 0, 0, 0, 115, 115, 2, 0, 0, 0, 100, 100, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 20, 64, 0, 0, 0, 0, 0, 0, 24, 64, 0, 0, 0, 0, 0, 0, 16, 64, 20, 2, 0, 0, 0, 0, 0, 0, 0, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 2, 0, 0, 0, 0, 0, 2, -1, -1, -1, -1, 18, 2, 0, 0, 0, 0, 0, 24, 3, 2, 0, 0, 0, 24, 17, 1, 24, 17, 1, 24, 3, 2, 0, 0, 0, 24, 17, 1, 24, 17, 0, 22, 2, 0, 0, 0, 2, 0, 0, 0, 115, 115, 2, 0, 0, 0, 100, 100, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 20, 64, 0, 0, 0, 0, 0, 0, 24, 64, 0, 0, 0, 0, 0, 0, 16, 64, 20, 2, 0, 0, 0, 0, 0, 0, 0, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 2, 0, 0, 0, 0, 0, 2, -1, -1, -1, -1, 18, 2, 0, 0, 0, 0, 0, 21, 9, 0, 0, 0, 110, 105, 101, 32, 115, 117, 112, 101, 114];
        foreach($javaByteArray as $pos=>$val){
            if($val<0){
                $javaByteArray[$pos]=$val+256;
            }
        }
        $ser = new com\mostka\serializer\Serializer($str);
        $phpByteArray = $ser->getReadBuffer();
        self::assert_($javaByteArray,$phpByteArray,'java to php byte conversion');
        return $ser;
    }

    private static function assert_($a,$b,$m){
        if (($b===null && $a===true) || ($a==$b)){
            echo "\t[OK] ".$m.PHP_EOL;
        }else{
            if (is_array($a) || is_object($a)){
                echo "[ERROR] ".$m.PHP_EOL.
                    json_encode($a).PHP_EOL.
                    json_encode($b).PHP_EOL;
            }
            echo "[ERROR] {$a}!={$b} {$m}";
        }
    }
}

Main::mains($argv);